package factotyService.trance;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import service.ImpUserServiceByServlet;
import service.UserService;
import util.Result;
import util.ServletUtil;
import entity.Blog_User;
import factotyService.AbstractService;

public class Regist extends AbstractService {
	//private Result result;
	private static UserService service = new ImpUserServiceByServlet();//可用工厂方法生产实例
	public Regist(){}
	public Regist(HttpServletRequest req, HttpServletResponse res) throws IOException, JSONException{
		super(req, res);
	}
	
	@Override
	public void check(){
		// TODO Auto-generated method stub
		String user_id = null;
		String user_password = null;
		String code = null;
		String user_phone = null;
		String user_email = null;
		String sessionCode = null;
		Blog_User user = new Blog_User();
		try {
			user_id = this.data.getString("user_id");
			user_password = this.data.getString("user_password");
			code = this.data.getString("key_code");
			user_phone = this.data.getString("user_phone");
			user_email = this.data.getString("user_email");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			System.out.println("Json异常");
			e.printStackTrace();
		}
		
		user.setUser_id(user_id);
		user.setUser_password(user_password);
		user.setUser_phone(user_phone);
		user.setUser_email(user_email);
		
		sessionCode = this.req.getSession().getAttribute("imgCode").toString();
		if(!(""+code).toLowerCase().equals(sessionCode.toLowerCase())){
			this.result = new Result("0000","验证码错误","");
		} else {
			//校验用户ID可否注册
			this.result = service.regist_check_user(user);
		}
		if(!"0001".equals(this.result.getCode())){
			this.result.setData(ServletUtil.update_img(req));
		}
	}

	@Override
	public void hand() {
		// TODO Auto-generated method stub
		if(!"0001".equals(this.result.getCode())){
			return;
		}
		Blog_User user = (Blog_User) this.result.getData();
		this.result = service.addUser(user);
	}
	
}
